import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @作者: songjing20
 * @时间: 2024/7/25
 * @测试点: jdbc驱动连接数据库，支持point使用最小值操作
 */

public class Opengauss_Function_Datatype_Point_Case0007 {
    public static Connection connectToDatabase(String[] args) {
        if (args.length < 4) {
            System.out.print("need para: host:port, dbname, user, password");
            return null;
        } else {
            Connection connection = null;
            try {
                String url = "jdbc:postgresql://" + args[0] + "/" + args[1];
                connection = DriverManager.getConnection(url, args[2], args[3]);
            } catch (Exception var10) {
                var10.printStackTrace();
                return null;
            }
            return connection;
        }
    }

    public static void main(String[] args) {
        try (Connection conn = connectToDatabase(args)) {
            // 建表，point类型字段
            try (Statement stmt = conn.createStatement()) {
                stmt.executeUpdate("drop table if exists t_point_0007 cascade;" +
                        "create table t_point_0007(c1 point);");
            }
            // 插入point数据
            try (Statement stmt = conn.createStatement()) {
                stmt.executeUpdate("insert into t_point_0007 values(point '(1,1)');");
                stmt.executeUpdate("insert into t_point_0007 values(point '(1.25,1.36)');");
                stmt.executeUpdate("insert into t_point_0007 values(point '(1000,0.1231)');");
                stmt.executeUpdate("insert into t_point_0007 values(point '(-1.79E+308, -1.79E+308)');");
            }
            // 查询数据
            String query = "SELECT * FROM t_point_0007;";
            try (PreparedStatement pstmt = conn.prepareStatement(query)) {
                try (ResultSet rs = pstmt.executeQuery()) {
                    while (rs.next()) {
                        System.out.println(rs.getObject(1));
                    }
                }
            }
            // 删表
            try (Statement stmt = conn.createStatement()) {
                stmt.executeUpdate("drop table if exists t_point_0007 cascade;");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}